package com.suep.course.controller;

import com.suep.course.dto.CourseDto;
import com.suep.course.dto.addCourseRequestDto;
import com.suep.course.dto.updateCourseRequestDto;
import com.suep.course.service.CourseManagementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@AllArgsConstructor
@RestController
@RequestMapping("api/course")
public class CourseManagementController {

    private final CourseManagementService courseService;
    //swagger annotation
    @Operation(
            summary = "根据课程编号查询课程信息",
            description = "根据课程编号查询单门课程",
            parameters = {
                    @Parameter(name = "id", description = "编号", required = true, examples = {
                            @ExampleObject(value = "1", description = "课程编号为1的课程信息"),
                    })
            },
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功获取课程信息",content = @Content(schema = @Schema(implementation = CourseDto.class))),
                    @ApiResponse(responseCode = "404",description = "课程信息不存在",content = @Content)
            }
    )
    //spring annotation
    @GetMapping("/")
    public String home() {
        return "Welcome to the application!";
    }
    @GetMapping("/get/{id}")
    public ResponseEntity<CourseDto> findCourserById(@PathVariable(name = "id") Integer id) {
        var course = courseService.getCourseById(id);
        if (course == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(course);
    }
    //获取所有老师信息
    @GetMapping("/get/all")
    public List<CourseDto> findAllCourses() {
        return this.courseService.getAllCourses();
    }
    //插入新老师信息
    @Operation(summary = "新增课程信息", description = "新增一个课程信息")
    @PostMapping("/add")
    public ResponseEntity<?> addCourse(
            @RequestBody addCourseRequestDto addcourseRequestDto,
            BindingResult bindingResult){
        //request validation
        if(bindingResult.hasErrors()){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(this.buildValidationError(bindingResult));
        }

        var result =this.courseService.addCourse(addcourseRequestDto);
        if(result.equals("success")){
            return ResponseEntity.ok().build();
        }else{
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
    private List<String> buildValidationError(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return bindingResult.getFieldErrors().stream()
                    .map(error -> error.getField() + ": " + error.getDefaultMessage()).toList();
        }
        return null;
    }
    //删除指定老师
    @Operation(summary = "删除课程信息", description = "删除一个课程信息")
    @DeleteMapping(path="/delete/{id}")
    public void removeCourse(@PathVariable(name = "id") Integer id) {
        this.courseService.removeCourse(id);
    }
    //更新指定老师信息
    @Operation(summary = "更新课程信息", description = "更新一个课程信息")
    @PostMapping(path="/update/{id}")
    public ResponseEntity<?> updateCourse(
            @PathVariable(name = "id") Integer id,
            @RequestBody updateCourseRequestDto updatecourseRequestDto,
            BindingResult bindingResult){
        //request validation
        if(bindingResult.hasErrors()){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(this.buildValidationError(bindingResult));
        }
        var result =this.courseService.updateCourse(id,updatecourseRequestDto);
        if(result.equals("success")){
            return ResponseEntity.ok().build();
        }else{
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

}
